package com.htx.oldbookpavilion.service.authorityservice.impl;

import com.htx.oldbookpavilion.entity.UserInfo;
import com.htx.oldbookpavilion.entity.UserInfoExample;
import com.htx.oldbookpavilion.mapper.UserInfoMapper;
import com.htx.oldbookpavilion.common.UidGenService;
import com.htx.oldbookpavilion.common.dict.UserAuthorityDict;
import com.htx.oldbookpavilion.service.authorityservice.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    private Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UidGenService uidGenService;

    /**
     * 校验用户名是否重复
     * @param userInfo
     * @return
     */
    @Override
    public boolean validateUsername(UserInfo userInfo) {
        UserInfoExample userInfoExample = new UserInfoExample();
        if (userInfo.getUserName() != null) {
            userInfoExample.or().andUserNameEqualTo(userInfo.getUserName());
        }
        if (userInfo.getPhoneNum() != null) {
            userInfoExample.or().andPhoneNumEqualTo(userInfo.getPhoneNum());
        }
        if(userInfo.getMailAddr() != null){
            userInfoExample.or().andMailAddrEqualTo(userInfo.getMailAddr());
        }
        long l = userInfoMapper.countByExample(userInfoExample);
        if(l > 0){
            return false;
        }
        return true;
    }

    /**
     * 注册新用户
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo register(UserInfo userInfo, boolean normal) {
        long userInfoId = uidGenService.getUid();
        userInfo.setUserInfoId(userInfoId);
        if (normal) {
            userInfo.setAuthority(UserAuthorityDict.NORMAL_USER);
        }else{
            userInfo.setAuthority(UserAuthorityDict.MANAGER);
        }
        userInfo.setCreateTime(new Date());
        int insertCount = userInfoMapper.insert(userInfo);
        if (insertCount == 1) {
            return userInfo;
        }
        return null;
    }

    /**
     * 用户登录
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo login(UserInfo userInfo) {
        UserInfoExample userInfoExample = new UserInfoExample();
        if (userInfo != null && userInfo.getUserName() != null && userInfo.getPassword() != null) {
            userInfoExample.or().andUserNameEqualTo(userInfo.getUserName())
                    .andPasswordEqualTo(userInfo.getPassword());
            userInfoExample.or().andPhoneNumEqualTo(userInfo.getUserName())
                    .andPasswordEqualTo(userInfo.getPassword());
        }
        List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
        if (userInfos != null && userInfos.size() > 0) {
            return userInfos.get(0);
        }else{
            return null;
        }
    }

    /**
     * 编辑修改个人信息
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo updateUserInfo(UserInfo userInfo){
        userInfo.setLastModifiedTime(new Date());
        userInfo.setLastModifier(userInfo.getUserName());
        if (userInfo.getPassword().equals("")){
            userInfo.setPassword(null);
        }
        int count = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if(count > 0){
            return userInfo;
        }
        return null;
    }

    /**
     * 根据id获取用户信息
     * @param ids
     * @return
     */
    @Override
    public List<UserInfo> getUserInfoByIds(String[] ids) {
        UserInfoExample userInfoExample = new UserInfoExample();
        if(ids == null || ids.length == 0){
            logger.warn("getUserInfoByIds:传递的ids为空或者null");
            return null;
        }
        for (String id : ids) {
            userInfoExample.or().andUserInfoIdEqualTo(Long.valueOf(id));
        }
        List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
        return userInfos;
    }
}
